/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#ifndef __ARCH_MODULES_CODESERVER_H
#define __ARCH_MODULES_CODESERVER_H

#include <iostream>
#include "module.h"
#include "arch.h"
#include "arch_c_compat.h"
#include "../../opal/codeserver/codeserver.h"

//namespace sleepyface::arch::modules
namespace sleepyface
{
    namespace arch
    {
        namespace modules
        {
            //class CodeServer: code server module
            class CodeServer : public ArchModule
            {
            public:
                const static int signalConfiguration = signalLimit+1; //memberof CodeServer: edit configuration
                const static int signalCompileFile = signalLimit+2; //memberof CodeServer: compile a file
                const static int signalCompileLine = signalLimit+3; //memberof CodeServer: compile a line
                const static int signalUnJIT = signalLimit+4; //memberof CodeServer: unjit a function
                const static int signalShell = signalLimit+5; //memberof CodeServer: run shell in foreground
                const static int signalShellBackground = signalLimit+6; //memberof CodeServer: run shell in background
                const static int signalLinkExternal = signalLimit+7; //memberof CodeServer: link an external function

            private:
                vm::VM virtualMachine;
                opal::CodeServer server;

                int linkExternal(string proto, void *func);
                int macroExternal(string n, string b);
                void linkInternalLibraries();
                void linkStandardLibrary();
                opal::AST dynamicTreeToAST(z_data *t);
                z_data *ASTToDynamicTree(opal::AST a);

            public:
                CodeServer();
                virtual int signal(ModuleSignal s, Architecture &a);
                virtual ModuleValue get(string k0, string k1, Architecture &a);
                virtual ModuleValue set(string k0, string k1, ModuleValue v, Architecture &a);
                virtual vector<string> list(Architecture &a);
            };
        }
    }
}

/// This is probably the most complex module currently. To set a configuration value, send a signalConfiguration with a string argument for the key and a string argument for the value. To compile a file, send a signalCompileFile with the path as a string argument. To compile a single line of code, send a signalCompileLine with the name of the function as one argument and the code as another string argument. To un-JIT a function, send a signalUnJIT with the name as an argument. To run the shell in the foreground, send a signalShellBackground. signalShellBackground does the same thing, but in the background. To link an external function to an internal prototype, send a signalLinkExternal with two arguments: the prototype as a string, and a pointer to the external function like a callback.
///
/// Getting <FUNCTION NAME>.ast will retrieve a dynamic AST tree. Setting <ANYTHING>.ast with a dynamic AST tree will compile a new function from the tree.

#endif